﻿Public Class SystemAdministration
    Inherits PageTemplate

    Private _isLoading As Boolean = False

    Private Sub SystemSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Title = "System Settings"
        Description = "Maintain confirm source file and archive database settings."
        PageIcon = My.Resources.Config
    End Sub

    Private Sub btnBrowseRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseRoot.Click
        SelectPath(txtRootFolder)
    End Sub

    Private Sub SelectPath(ByVal txtBox As TextBox)

        folderBrowserDialog.SelectedPath = txtBox.Text
        If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
            txtBox.Text = folderBrowserDialog.SelectedPath
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveSettings()
        GoToPage(FormTypes.Home)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        GoToPage(FormTypes.Home)
    End Sub

    Private Sub LoadSettings()
        _isLoading = True
        With Engine.SystemSettings.Instance
            Dim databaseFilePath As String = String.Empty
            Dim server As String = String.Empty
            Dim database As String = String.Empty

            Dim configHelper As New ConfigurationHelper()
            configHelper.GetDatabaseSettings(databaseFilePath, server, database)
            If String.IsNullOrEmpty(databaseFilePath) Then
                txtDatabasePath.Text = String.Format("Server={0} Database={1}", server, database)
            Else
                txtDatabasePath.Text = databaseFilePath
            End If

            txtRootFolder.Text = .DataFilePath

            txtTSConfirmFileName.Tag = .TSConfirmFileName
            txtAdventFileName.Tag = .AdventConfirmFileName
            txtCFConfirmFilePath.Tag = .AplConfirmFileName
            txtCFMatchFileName.Tag = .MatchFileName
            txtCFAccountID.Tag = .AccountID
            txtAplUserID.Tag = .FtpUsername
            txtAplPassword.Tag = .FtpPassword
            txtFtpServer.Tag = .FtpServer
            txtChangeDirectory.Tag = .FtpChangeDirectory

            chkTradeSuite.Checked = Not String.IsNullOrEmpty(.TSConfirmFileName)
            chkCheckFree.Checked = Not String.IsNullOrEmpty(.AplConfirmFileName)
            chkAdvent.Checked = Not String.IsNullOrEmpty(.AdventConfirmFileName)

            SetTSAdventControlState(chkTradeSuite.Checked, txtTSConfirmFileName)
            SetTSAdventControlState(chkAdvent.Checked, txtAdventFileName)
            SetCFControlState()
        End With
        _isLoading = False
    End Sub

    Private Sub SaveSettings()
        With Engine.SystemSettings.Instance
            .DataFilePath = txtRootFolder.Text
            .TSConfirmFileName = txtTSConfirmFileName.Text
            .AplConfirmFileName = txtCFConfirmFilePath.Text
            .AdventConfirmFileName = txtAdventFileName.Text
            .MatchFileName = txtCFMatchFileName.Text
            .AccountID = txtCFAccountID.Text
            .FtpUsername = txtAplUserID.Text
            .FtpPassword = txtAplPassword.Text
            .FtpServer = txtFtpServer.Text
            .FtpChangeDirectory = txtChangeDirectory.Text
            .Save()
        End With
    End Sub

    Private Sub chkTradeSuite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTradeSuite.CheckedChanged
        If _isLoading Then Return

        SetTSAdventControlState(chkTradeSuite.Checked, txtTSConfirmFileName)
    End Sub

    Private Sub chkAdvent_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAdvent.CheckedChanged
        If _isLoading Then Return
        SetTSAdventControlState(chkAdvent.Checked, txtAdventFileName)
    End Sub

    Private Sub chkCheckFree_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCheckFree.CheckedChanged
        SetCFControlState()
    End Sub

    Private Sub SetTSAdventControlState(ByVal checked As Boolean, ByVal txtBox As TextBox)
        If checked Then
            EnableTextBox(txtBox)
        Else
            DisableTextBox(txtBox)
        End If
    End Sub

    Private Sub SetCFControlState()
        If chkCheckFree.Checked Then
            EnableTextBox(txtCFConfirmFilePath)
            EnableTextBox(txtCFAccountID)
            EnableTextBox(txtCFMatchFileName)
            EnableTextBox(txtChangeDirectory)
            EnableTextBox(txtAplUserID)
            EnableTextBox(txtAplPassword)
            EnableTextBox(txtFtpServer)
        Else
            DisableTextBox(txtCFConfirmFilePath)
            DisableTextBox(txtCFAccountID)
            DisableTextBox(txtCFMatchFileName)
            DisableTextBox(txtChangeDirectory)
            DisableTextBox(txtAplUserID)
            DisableTextBox(txtAplPassword)
            DisableTextBox(txtFtpServer)
        End If
    End Sub

    Private Sub EnableTextBox(ByVal txtBox As TextBox)
        txtBox.Enabled = True
        If String.IsNullOrEmpty(CStr(txtBox.Tag)) Then
            txtBox.Text = ""
        Else
            txtBox.Text = txtBox.Tag.ToString()
        End If
    End Sub

    Private Sub DisableTextBox(ByVal txtBox As TextBox)
        txtBox.Enabled = False
        txtBox.Tag = txtBox.Text
        txtBox.Text = ""
    End Sub

    Private Sub SystemAdministration_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter
        LoadSettings()
    End Sub

End Class